package com.test.daily.leetcode.y2021.m10.day1025;

/**
 * @descriptions:
 * @author: Tom
 * @date: 2021/10/25 上午 11:50
 * @version: 1.0
 */
public class Code01_SearchMatrix {
    public static void main(String[] args) {
        Code01_SearchMatrix cs = new Code01_SearchMatrix();
        int[][] matrix = {
                {1, 4, 7, 11, 15},
                {2, 5, 8, 12, 19},
                {3, 6, 9, 16, 22},
                {10, 13, 14, 17, 24},
                {18, 21, 23, 26, 30}};
        System.out.println(cs.searchMatrix(matrix, 30));
    }

    public boolean searchMatrix(int[][] matrix, int target) {
        int m = matrix.length;
        int n = matrix[0].length;
        int x = n - 1;
        int y = 0;
        while(x >= 0 && y < m){
            if(matrix[y][x] > target){
                x--;
            } else if(matrix[y][x] < target){
                y++;
            } else {
                return true;
            }
        }
        return false;
    }
}
